package com.reder.practice.controller;

import com.reder.practice.bean.LoginInfo;
import com.reder.practice.bean.User;
import com.reder.practice.common.annotation.FromSession;
import com.reder.practice.common.dto.ServerResponse;
import com.reder.practice.common.util.PageUtil;
import com.reder.practice.common.util.SessionUtil;
import com.reder.practice.service.UserService;
import com.reder.practice.service.VideoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户 API 层
 *
 * @author Red
 * email: 1318944013@qq.com
 * date: 2019/9/5 9:40
 */
@ApiOperation("前台用户 API")
@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Resource
    private UserService userService = null;

    @Resource
    private VideoService videoService = null;

    @ApiOperation("分页列出所有用户")
    @RequestMapping(method = RequestMethod.GET, path = "/list")
    public ServerResponse listUsers(@ApiParam("第几页数据，默认为 1") @RequestParam(defaultValue = 1 + "") int pageNumber,
                                    @ApiParam("一页显示多少条数据，默认为 12") @RequestParam(defaultValue = PageUtil.DEFAULT_SIZE_OF_ONE_PAGE + "") int sizeOfOnePage) {
        return userService.listUsers(pageNumber, sizeOfOnePage);
    }

    @ApiOperation("用户注册")
    @RequestMapping(method = RequestMethod.POST, path = "/register")
    public ServerResponse register(@ApiParam("用户昵称，需要检查昵称是否存在") String nickname,
                                   @ApiParam("密码") String password) {
        return userService.register(nickname, password);
    }

    @ApiOperation("判断用户昵称是否存在")
    @RequestMapping(method = RequestMethod.GET, path = "/exists")
    public ServerResponse isNicknameExisted(@ApiParam("要被判断的用户昵称") String nickname) {
        return userService.isNicknameExisted(nickname);
    }

    @ApiOperation("用户登录")
    @RequestMapping(method = RequestMethod.POST, path = "/login")
    @SuppressWarnings("unchecked")
    public ServerResponse login(@ApiParam("用户昵称") String nickname,
                                @ApiParam("密码") String password,
                                @ApiIgnore HttpSession session) {
        ServerResponse<LoginInfo> response = userService.login(nickname, password);
        if (response.isSuccess()) {
            SessionUtil.putLoginInfoToSession(response.getData(), session);
        }
        return response;
    }

    @ApiOperation("根据用户昵称查询用户信息")
    @RequestMapping(method = RequestMethod.GET, path = "/byNickname")
    public ServerResponse getUser(@ApiParam("要被查询的用户的昵称") String nickname) {
        return userService.getUser(nickname);
    }

    @ApiOperation("根据用户 id 查询用户信息")
    @RequestMapping(method = RequestMethod.GET, path = "/byId")
    public ServerResponse getUser(@ApiParam("要被查询的用户的 id") long id) {
        return userService.getUser(id);
    }

    @GetMapping("/videos")
    @ApiOperation("展示个人视频列表")
    public ServerResponse listVideo(int pageNumber,
                                    @RequestParam(defaultValue = PageUtil.DEFAULT_SIZE_OF_ONE_PAGE + "") int sizeOfOnePage,
                                    @FromSession User user) {

        return videoService.listVideoById(pageNumber, sizeOfOnePage, user);
    }
}
